/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.app.service.impl;

import java.io.File;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.yanlight.Constants;
import com.yanlight.app.dal.dao.AppVersionDAO;
import com.yanlight.app.dal.dto.AppVersionPageQuery;
import com.yanlight.app.dal.dto.AppVersionUniqueQuery;
import com.yanlight.app.dal.entity.AppVersion;
import com.yanlight.app.model.AppVersionSpec;
import com.yanlight.app.request.AppVersionAddRequest;
import com.yanlight.app.request.AppVersionModifyRequest;
import com.yanlight.app.request.AppVersionQueryRequest;
import com.yanlight.app.response.AppVersionDetailResponse;
import com.yanlight.app.response.AppVersionListResponse;
import com.yanlight.app.service.AppVersionService;
import com.yanlight.exception.ErrorType;
import com.yanlight.exception.TradeException;
import com.yanlight.util.DateTimeUtil;
import com.yanlight.util.FileUploadUtil;
import com.yanlight.util.ToolsUtil;

/**
 * 
 * @author haolw
 * @time 2017/12/25
 */
@Service
public class AppVersionServiceImpl implements AppVersionService {
	@Autowired
	private AppVersionDAO appVersionDAO;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(AppVersionAddRequest request) {
		String version = request.getVersion();
		Integer edition = request.getEdition();
		String linkHref = request.getLinkHref();
		Integer terminalType = request.getTerminalType();
		Integer osType = request.getOsType();
		AppVersionUniqueQuery query = new AppVersionUniqueQuery();
		query.setOsType(osType);
		query.setTerminalType(terminalType);
		query.setVersion(version);
		query.setEdition(edition);
		AppVersion uniqueApp = appVersionDAO.findUnique(query);
		if (uniqueApp != null) {
			throw TradeException.error(ErrorType.REPEATED_DATA);
		}
		AppVersion appVersion = new AppVersion();
		BeanUtils.copyProperties(request, appVersion);
		if (StringUtils.isBlank(linkHref)) {
			MultipartFile file = request.getFile();
			String path = FileUploadUtil.processApk(version, terminalType, file);
			appVersion.setLinkHref(ToolsUtil.wrapApp(path));
		}
		appVersion.setCreateTime(DateTimeUtil.getNowTime());
		appVersionDAO.save(appVersion);
	}

	@Override
	public AppVersionListResponse findList(AppVersionQueryRequest request) {
		AppVersionListResponse response = new AppVersionListResponse();
		response.setPageSize(request.getPageSize());
		response.setTotalRecord(0);
		AppVersionPageQuery condition = new AppVersionPageQuery();
		BeanUtils.copyProperties(request, condition);
		int count = appVersionDAO.count(condition);
		if (count > 0) {
			List<AppVersion> list = appVersionDAO.findPageList(condition);
			AppVersionSpec spec;
			for (AppVersion appVersion : list) {
				spec = new AppVersionSpec();
				BeanUtils.copyProperties(appVersion, spec);
				response.addSpec(spec);
			}
			response.setTotalRecord(count);
		}
		return response;
	}

	@Override
	public AppVersionDetailResponse findDetail(Integer id) {
		AppVersionDetailResponse response = new AppVersionDetailResponse();
		AppVersion appVersion = appVersionDAO.findById(id);
		if (appVersion != null) {
			BeanUtils.copyProperties(appVersion, response);
		} else {
			throw TradeException.error(ErrorType.NOT_EXIST_RECORD);
		}
		return response;
	}

	@Override
	public void modify(AppVersionModifyRequest request) {
		Integer id = request.getId();
		AppVersion appVersion = appVersionDAO.findById(id);
		if (appVersion != null) {
			BeanUtils.copyProperties(request, appVersion);
		} else {
			throw TradeException.error(ErrorType.NOT_EXIST_RECORD);
		}
		appVersionDAO.update(appVersion);
	}

	@Override
	public void delete(Integer id) {
		AppVersion app = appVersionDAO.findById(id);
		if (app != null) {
			String linkHref = app.getLinkHref();
			String host = Constants.APP_FILE_HOST;
			String base = Constants.UPLOAD_APP_PATH;
			String path = StringUtils.replace(linkHref, host, base);
			File appFile = new File(path);
			if (appFile.exists()) {
				appFile.delete();
			}
			appVersionDAO.delete(id);
		}
	}

}
